package com.hengxinguotong.zhihuichengjian.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.hengxinguotong.zhihuichengjian.R;
import com.hengxinguotong.zhihuichengjian.bean.User;
import com.hengxinguotong.zhihuichengjian.treeview.RecyclerAdapter;
import com.hengxinguotong.zhihuichengjian.widget.font.HXTextView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @CopyRight 恒信国通信息科技
 * @Author WuLei
 * @Date 2019/1/22
 */

public class EnterpriseUserAdapter extends RecyclerAdapter<User, EnterpriseUserAdapter.EnterpriseUserViewHolder> {
    private OnChildDeleteClickListener onChildDeleteClickListener;
    private OnChildHeadClickListener onChildHeadClickListener;
    private int mPosition;
    private boolean hasDelete = false;

    public EnterpriseUserAdapter(Context context, List<User> dataList) {
        super(context, dataList);
    }

    @Override
    public EnterpriseUserViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = View.inflate(context, R.layout.item_enterprise_user, null);
        return new EnterpriseUserViewHolder(view);
    }

    public void setHasDelete(boolean hasDelete) {
        this.hasDelete = hasDelete;
    }

    public void setmPosition(int mPosition) {
        this.mPosition = mPosition;
    }

    public void setOnChildDeleteClickListener(OnChildDeleteClickListener onChildDeleteClickListener) {
        this.onChildDeleteClickListener = onChildDeleteClickListener;
    }

    public void setOnChildHeadClickListener(OnChildHeadClickListener onChildHeadClickListener) {
        this.onChildHeadClickListener = onChildHeadClickListener;
    }

    @Override
    public void onBindViewHolder(EnterpriseUserViewHolder holder, int position) {
        holder.bindData(position);
    }

    public class EnterpriseUserViewHolder extends RecyclerAdapter.BaseViewHolder<User>{
        @BindView(R.id.head_portrait_img)
        ImageView headPortraitImg;
        @BindView(R.id.delete_img)
        ImageView deleteImg;
        @BindView(R.id.user_name)
        HXTextView userName;

        public EnterpriseUserViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void bindData(final int position) {
            User user = dataList.get(position);
            Glide.with(context)
                    .load(user.getHeadPortrait())
                    .placeholder(R.mipmap.icon_head_potrait)
                    .error(R.mipmap.icon_head_potrait)
                    .into(headPortraitImg);
            userName.setText(user.getName());
            if(hasDelete){
                deleteImg.setVisibility(View.VISIBLE);
            }else {
                deleteImg.setVisibility(View.GONE);
            }
            headPortraitImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onChildHeadClickListener.onChildHeadClick(mPosition, position);
                }
            });
            deleteImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onChildDeleteClickListener.onChildDeleteClick(mPosition, position);
                }
            });
        }
    }

    public interface OnChildHeadClickListener{
        void onChildHeadClick(int position, int childPosition);
    }

    public interface OnChildDeleteClickListener{
        void onChildDeleteClick(int position, int childPosition);
    }
}
